#include "ResultJudge.h"
#include "DatabaseMgmt.h"


CResultJudge::CResultJudge()
{

}

CResultJudge::~CResultJudge()
{

}

int CResultJudge::queryByReportUid(uint32_t report_id, ResultJudge& result)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT UID,REPORT_UID,RESULT_TYPE,RESULT_DRSCRIBE"
		" FROM TB_RESULT_JUDGE WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		if (query.next())
		{
			result.uid = query.value("UID").toUInt();
			result.report_uid = query.value("REPORT_UID").toUInt();
			result.result_type = query.value("RESULT_TYPE").toUInt();
			result.result_describe = query.value("RESULT_DRSCRIBE").toString();
		}
	}

	return ret;
}

int CResultJudge::removeByReportUid(uint32_t report_id)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_RESULT_JUDGE WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CResultJudge::modifyByReportUid(ResultJudge result)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("UPDATE TB_RESULT_JUDGE SET RESULT_TYPE=%1,RESULT_DRSCRIBE='%2'"
		" WHERE REPORT_UID=%3")
		.arg(result.result_type)
		.arg(result.result_describe)
		.arg(result.report_uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CResultJudge::insertRow(ResultJudge result)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("INSERT INTO TB_RESULT_JUDGE (REPORT_UID,RESULT_TYPE,RESULT_DRSCRIBE)"
		" VALUES (%1,%2,'%3');")
		.arg(result.report_uid)
		.arg(result.result_type)
		.arg(result.result_describe);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	return ret;
}
